// 计算连个时间格式间相差多少天
export const DateDiff = (sDate1, sDate2) => { //sDate1和sDate2是2002-12-18格式

    let date1 = (sDate1 + '').replace(/-/g, '/')
    let date2 = (sDate2 + '').replace(/-/g, '/')
    // console.log(11111111,date1,date2);
    const sDate = Date.parse(date1)
    const eDate = Date.parse(date2)
    // console.log(22222222,sDate,eDate);
    if (sDate > eDate) {
        return 0
    }
    // 这个判断可以根据需求来确定是否需要加上
    if (sDate === eDate) {
        return 1
    }
    const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
    return days

}
//根据时间获取对应文本
export const getDayOrWeek = (date) => {
    // console.log('date',date);return
    if (date == null) return ''
    date = new Date(date)

    // 星期数组
    const weekArr = ['日', '一', '二', '三', '四', '五', '六']

    // 获取传入日期与当前日期相差的天数，一天 86400000 毫秒
    const diffTime = (date.setHours(0, 0, 0, 0) - new Date().setHours(0, 0, 0, 0)) / 86400000

    // 获取“前天/昨天/今天/明天/后天”，若不在这些里面则显示星期
    return `周${weekArr[date.getDay()]}`
    // return dayArr[diffTime + 2] || `周${weekArr[date.getDay()]}`
}

//获取指定日期加上指定日月年
export const DateAdd = (interval, date, number) => {
    if (typeof number !== 'number'){
        number = Number(number)
    }
    /*
     *   功能:实现javascript的DateAdd功能.
     *   参数:interval,字符串表达式，表示要添加的时间间隔.
     *   参数:number,数值表达式，表示要添加的时间间隔的个数.
     *   参数:date,时间对象.
     *   返回:新的时间格式.
     *   var   now   =   new   Date();
     *   var   newDate   =   DateAdd( "d ",5,now);
     *---------------   DateAdd(interval,number,date)   -----------------
     */
    let sdate = new Date(date)
    date = new Date(date)
    switch (interval) {
        case "year": { //加年
            date.setFullYear(date.getFullYear() + number);
            break;
        }
        case "quarter": { //加季度
            date.setMonth(date.getMonth() + number * 3);
            break;
        }
        case "month": { //加月
            let day = sdate.getDate()
            date.setMonth(date.getMonth() + number, 1)
            let mo = date.getMonth() + 1
            if (mo == 4 || mo == 6 || mo == 9 || mo == 11) {
                if (day > 30) {
                    day = 30
                }
            } else if (mo == 2) {
                if (isLeapYear(date.getFullYear())) {
                    if (day > 29) {
                        day = 29
                    } else {
                        day = 28
                    }
                }
            } else {
                if (day > 31) {
                    day = 31
                }
            }
            date = new Date(date.getFullYear(), date.getMonth(), day)
            break;
        }
        case "week": { //加星期
            date.setDate(date.getDate() + number * 7);
            break;
        }
        case "day": { //加天
            date.setDate(date.getDate() + number);
            break;
        }
        default: {
            date.setDate(date.getDate() + number);
            break;
        }
    }
    return dateToFormat(date, 'yyyy-mm-dd')
    // return uni.$u.timeFormat(date, 'yyyy-mm-dd');
}


export const DateCompare = (date1, type, date2) => {
    date1 = (new Date(date1)).valueOf()
    date2 = (new Date(date2)).valueOf()
    switch (type) {
        case '>':
            return !!(date1 > date2)
            break;
        case '>=':
            return !!(date1 >= date2)
            break;
        case '<':
            return !!(date1 < date2)
            break;
        case '<=':
            return !!(date1 <= date2)
            break;
        case '==':
            return !!(date1 == date2)
            break;
        case '!=':
            return !!(date1 != date2)
            break;
        default:
            break;
    }
}

// 比较日期时间大小
export const compareDateTime = (date1, date2, type) => {
    if (!date1 || !date2) return false
    date1 = new Date(date1)
    date2 = new Date(date2)
    switch (type) {
        case '==':
            return date1.getTime() === date2.getTime()
            break;
        case '!=':
            return date1.getTime() !== date2.getTime()
            break;
        case '>':
            return date1.getTime() > date2.getTime()
            break;
        case '>=':
            return date1.getTime() >= date2.getTime()
            break;
        case '<':
            return date1.getTime() < date2.getTime()
            break;
        case '<=':
            return date1.getTime() <= date2.getTime()
            break;
        default:
            break;
    }
}

//计算两个日期相差月份数
export const MonthDiff = (sDate, eDate) => {
    console.log(9999999, sDate, eDate);
    if (sDate && eDate) {
        sDate = new Date(sDate)
        eDate = new Date(eDate)

        let sDateYear = sDate.getFullYear()
        let sDateMonth = sDate.getMonth() + 1

        let eDateYear = eDate.getFullYear()
        let eDateMonth = eDate.getMonth() + 1
        return (eDateYear * 12 + eDateMonth) - (sDateYear * 12 + sDateMonth)
    }
    return ''
}

//时间戳转换成时间格式
export const dateToFormat = (date = null, formatStr = "yyyy-mm-dd") => {
    if (!date) {
        date = new Date()
    }
    date = new Date(date)
    const timeSource = {
        'y': date.getFullYear().toString(), // 年
        'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月
        'd': date.getDate().toString().padStart(2, '0'), // 日
        'h': date.getHours().toString().padStart(2, '0'), // 时
        'M': date.getMinutes().toString().padStart(2, '0'), // 分
        's': date.getSeconds().toString().padStart(2, '0') // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    }
    for (const key in timeSource) {
        const [ret] = new RegExp(`${key}+`).exec(formatStr) || []
        if (ret) {
            // 年可能只需展示两位
            const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0
            formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex))
        }
    }
    return formatStr
}
//获取指定月份天数
const getMonthDay = (date) => {
    if (!date) return ''
    date = new Date(date)
    let d = new Date(date.getFullYear(), date.getMonth() + 1, 0);
    return d.getDate()
}

//判断是否为润年
const isLeapYear = (year) => {
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        return true
    } else {
        return false
    }
}
//日期格式化
export const formatDate = (value = '2024-01-01', fmt = 'yyyy-MM-dd') => {
    var date = new Date(value.replace(/-/g, '/'))
    var o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "w+": date.getDay(), //星期
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        S: date.getMilliseconds(), //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
    for (var k in o) {
        if (k === "w+") {
            if (o[k] === 0) {
                fmt = fmt.replace("w", "周日");
            } else if (o[k] === 1) {
                fmt = fmt.replace("w", "周一");
            } else if (o[k] === 2) {
                fmt = fmt.replace("w", "周二");
            } else if (o[k] === 3) {
                fmt = fmt.replace("w", "周三");
            } else if (o[k] === 4) {
                fmt = fmt.replace("w", "周四");
            } else if (o[k] === 5) {
                fmt = fmt.replace("w", "周五");
            } else if (o[k] === 6) {
                fmt = fmt.replace("w", "周六");
            }
        } else if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
            );
        }
    }
    return fmt;
};
//日期格式化
export const formatCnDate = (value, fmt = 'yyyy-MM-dd') => {
    var date = new Date(value)
    var o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "w+": date.getDay(), //星期
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        S: date.getMilliseconds(), //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(
            RegExp.$1,
            (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
    for (var k in o) {
        if (k === "w+") {
            if (o[k] === 0) {
                fmt = fmt.replace("w", "周日");
            } else if (o[k] === 1) {
                fmt = fmt.replace("w", "周一");
            } else if (o[k] === 2) {
                fmt = fmt.replace("w", "周二");
            } else if (o[k] === 3) {
                fmt = fmt.replace("w", "周三");
            } else if (o[k] === 4) {
                fmt = fmt.replace("w", "周四");
            } else if (o[k] === 5) {
                fmt = fmt.replace("w", "周五");
            } else if (o[k] === 6) {
                fmt = fmt.replace("w", "周六");
            }
        } else if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(
                RegExp.$1,
                RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
            );
        }
    }
    return fmt;
};

//获取指定日期的名称 今天、明天、后天、昨天、前天 等
export const getDateName = (date) => {
    let currentDate = new Date(date)
    currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate())
    let now = new Date()
    let today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
    let diff = (currentDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24)
    if (diff == 0) {
        return '今天'
    } else if (diff == 1) {
        return '明天'
    } else if (diff == 2) {
        return '后天'
    } else if (diff == -1) {
        return '昨天'
    } else if (diff == -2) {
        return '前天'
    } else {
        return dateToFormat(date, 'yyyy-mm-dd')
    }
}

//获取日期范围
export const getDateRange = (start, end) => {
    let startDate = new Date(start)
    let endDate = new Date(end)
    let result = []
    let currentDate = startDate
    while (currentDate <= endDate) {
        result.push(dateToFormat(currentDate, 'yyyy-mm-dd'))
        currentDate = new Date(currentDate.getTime() + 24 * 60 * 60 * 1000)
    }
    return result
}
